﻿@{
    List<DataAccess.Models.Alimama> alimamas = new List<DataAccess.Models.Alimama>();
    alimamas = (List<DataAccess.Models.Alimama>)ViewBag.Alimamas;

    //List<DataAccess.Models.Category> categories = new List<DataAccess.Models.Category>();
    //categories = (List<DataAccess.Models.Category>)ViewBag.Categories;


    List<DataAccess.Models.AgencyUsers> users = new List<DataAccess.Models.AgencyUsers>();
    users = (List<DataAccess.Models.AgencyUsers>)ViewBag.Users;


    DataAccess.Models.AgencyUsers superior = (DataAccess.Models.AgencyUsers)ViewBag.superior;
    DataAccess.Models.AgencyUsers onthe_superior = (DataAccess.Models.AgencyUsers)ViewBag.onthe_superior;

}
@model  DataAccess.Models.AgencyUsers

<script src="~/Scripts/plugins/jquery.min.js"></script>
<script src="~/Scripts/plugins/layer/layer.min.js"></script>
@section Styles{
    <link href="//cdn.bootcss.com/bootstrap-select/1.12.2/css/bootstrap-select.min.css" rel="stylesheet" />
}
@section Scripts{
    <script src="~/Scripts/plugins/validate/jquery.validate.min.js"></script>
    <script src="~/Scripts/plugins/validate/messages_zh.min.js"></script>
    <script src="//cdn.bootcss.com/bootstrap-select/1.12.2/js/bootstrap-select.min.js"></script>
    <script type="text/javascript">
        var index = parent.layer.getFrameIndex(window.name);
        var reload = 0;
        $(function () {
            $("#cancel").click(function () {
                if (reload) {
                    parent.location.reload();
                }
                parent.layer.close(index);
            });
            //$(".chosen-select").chosen();

            $('#label').selectpicker({
                noneSelectedText: "默认类别",
            });
            $.validator.setDefaults({
                submitHandler: function () {
                    common.invokeApi(
                    "post",
                    "/setAgencyUser.html",
                    {
                        id: $("#id").val(), group_name: $("#group_name").val(), label: $("#label").val(), alimama_id: $("#alimama_id").val(),
                        group_wx_nickname: $("#group_wx_nickname").val(), group_robot_name: $("#group_robot_name").val(),
                        superior: $("#superior").val(), commission: $("#commission").val(), superior_commission: $("#superior_commission").val()
                    },
                    function () { },
                    function (data) {
                        common.msg(data.Message, 1, function () {
                            $("#promotion").html("代理：开启");

                            if ($("table").length < 2) {
                                var html = '<table class="table table-bordered table-striped">';
                                html += '<thead><tr>';
                                html += '<th colspan="2">网站PID：' + data.Datas.site_pid + '</th>';
                                html += '<th>QQPID：' + data.Datas.qq_pid + '</th>';
                                html += '<th>微信PID：' + data.Datas.wechat_pid + '</th>';
                                html += '</tr></thead></table>';
                                $("table").after(html);
                            }

                            reload = 1;
                        });
                    },
                    function (data) {
                        common.msg(data.Message, 2);
                    },
                    function () { }
                    );
                }
            });

            $("#form1").validate({
                rules: {
                    group_name: {
                        required: true,
                        rangelength: [1, 50]
                    },
                    group_wx_nickname: {
                        required: true,
                        rangelength: [1, 50],
                    },
                    group_robot_name: {
                        required: true,
                        rangelength: [1, 50],
                    },
                    alimama_id: {
                        required: true
                    },
                    commission: {
                        required: true,
                        digits: true,
                        range: [0, 100],
                        maxlength: 3
                    },
                    superior_commission: {
                        required: true,
                        digits: true,
                        range: [0, 100],
                        maxlength: 3
                    }
                },
                messages: {
                    group_name: "必须输入群名称,且不能大于50个字符",
                    group_wx_nickname: "必须输入群管微信昵称,且不能大于50个字符",
                    group_robot_name: "必须输入群管机器人昵称,且不能大于50个字符",
                    alimama_id: "请选择联盟账号",
                    commission: "请输入0-100的整数",
                    superior_commission: "请输入0-100的整数",
                }
            })
        });
    </script>
    <script src="~/Scripts/plugins/validate/jquery.validate.min.js"></script>
    <script src="~/Scripts/plugins/validate/messages_zh.min.js"></script>
    <script src="~/Scripts/plugins/bootstrap.min.js"></script>
    <script src="~/Scripts/plugins/content.min.js"></script>
    <script src="~/Scripts/sys/lib/common_raw.js"></script>
}







<div class="row">
    <div class="col-lg-12">
        <div class="row">
            <div class="col-lg-12">
                <ol class="breadcrumb">
                    <li><a href="#">代理管理</a></li>
                    <li class="active"><span>代理设置</span></li>
                </ol>
                <h1>代理设置</h1>
            </div>
        </div>

        <div class="row">
            <div class="col-lg-12">
                <div class="main-box">
                    <header class="main-box-header clearfix">
                        <h2></h2>
                    </header>
                    <div class="main-box-body clearfix">
                        <div class="table-responsive">
                            <table class="table table-bordered table-striped">
                                <thead>
                                    <tr>
                                        <th>
                                            账号：@Model.username
                                        </th>
                                        <th>
                                            姓名：@Model.nickname
                                        </th>
                                        <th>
                                            手机：@Model.tel
                                        </th>
                                    </tr>
                                    <tr>
                                        <th>
                                            状态：@(Model.state == 1 ? "启用" : "禁用")
                                        </th>
                                        <th id="promotion">
                                            代理：@(Model.promotion == 1 ? "开启" : "未开启")
                                        </th>
                                        <th>
                                            备注：@Model.remark
                                        </th>
                                    </tr>
                                </thead>
                            </table>
                            @{
                                if (Model.promotion == 1)
                                {
                                    <table class="table table-bordered table-striped">
                                        <thead>
                                            <tr>
                                                <th>网站PID：@(Model.site_pid)</th>
                                                <th>QQPID：@(Model.qq_pid)</th>
                                                <th>微信PID：@(Model.wechat_pid)</th>
                                            </tr>
                                        </thead>
                                    </table>
                                }
                            }
                        </div>
                        <form method="post" class="form-horizontal" id="form1">
                            <input type="hidden" id="id" name="id" value="@(Model.id)" />

                            <div class="form-group">
                                <label for="state" class="col-lg-2 control-label">联盟账号:</label>
                                <div class="col-lg-2">
                                    <select class="form-control" name="alimama_id" id="alimama_id">
                                        @{
                                            foreach (var item in alimamas)
                                            {
                                                <option value="@(item.alimamaid)" @(item.alimamaid == Model.alimamaid ? "selected" : "")>@(item.username)</option>
                                            }
                                        }
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="PermissionId" class="col-lg-2 control-label">分佣比例 %:</label>
                                <div class="col-lg-2">
                                    <input type="text" class="form-control" value="@(Model.promotion == 1 ? Model.commission : ViewBag.defaultCommission)" id="commission" name="commission">
                                </div>
                            </div>


                            <div class="form-group">
                                <label for="RoleId" class="col-lg-2 control-label">上级代理:</label>
                                <div class="col-lg-2">
                                    @{
                                        if (Model.superior != 0)
                                        {
                                            <input type="text" value="@(superior.username)" class="form-control" readonly="readonly">
                                            <input type="hidden" id="superior" name="superior" value="@(superior.id)" />
                                        }
                                        else
                                        {
                                            <select id="superior" name="superior" data-placeholder="选择代理..." class="chosen-select form-control" style="width:100%;">
                                                <option value="0" @(0 == Model.superior ? "selected" : "")>选择代理...</option>
                                                @{
                                            foreach (var item in users.FindAll(p => p.id != Model.id))
                                            {
                                                <option value="@(item.id)" @(item.id == Model.superior ? "selected" : "")>@(item.username)</option>
                                            }
                                                }
                                            </select>
                                        }
                                    }
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="RoleId" class="col-lg-2 control-label">上级佣金 %:</label>
                                <div class="col-lg-2">
                                    <input type="text" class="form-control" value="@(Model.promotion == 1 ? Model.superior_commission : ViewBag.defaultSuperior_commission)" id="superior_commission" name="superior_commission">
                                </div>
                            </div>


                            <div class="form-group">
                                <label for="RoleId" class="col-lg-2 control-label">上上级代理:</label>
                                <div class="col-lg-2">
                                    @{
                                        if (Model.onthe_superior != 0)
                                        {
                                            <input type="text" value="@(onthe_superior.username)" class="form-control" readonly="readonly">
                                            <input type="hidden" id="onthe_superior" name="onthe_superior" value="@(onthe_superior.id)"  />
                                            }
                                            else
                                            {
                                            <select id="onthe_superior" name="onthe_superior" data-placeholder="选择代理..." class="chosen-select form-control" style="width:100%;">
                                                <option value="0" @(0 == Model.onthe_superior ? "selected" : "")>选择代理...</option>

@{
    

                                            foreach (var item in users.FindAll(p => p.id != Model.id))
                                            {
                                                <option value="@(item.id)"  @(item.id == Model.onthe_superior ? "selected" : "")>@(item.username)</option>
                                            }
                                            }
                                        </select>
                                        }

                                    }

                                </div>
                            </div>

                            <div class="form-group">
                                <label for="RoleId" class="col-lg-2 control-label">上上级佣金 %:</label>
                                <div class="col-lg-2">
                                    <input type="text" class="form-control" value="@(Model.promotion == 1 ? Model.onthe_superior_commission : ViewBag.defaultSuperior_commission)" id="onthe_superior_commission" name="onthe_superior_commission">
                                </div>
                            </div>
                            @*<div class="form-group">
                                    <label for="RoleId" class="col-lg-2 control-label">群名称:</label>
                                    <div class="col-lg-2">
                                        <input type="text" id="group_name" name="group_name" value="@(Model.group_name)" class="form-control" placeholder="输入群名称">
                                    </div>
                                </div>*@
                            @*<div class="form-group">
                                    <label for="RoleId" class="col-lg-2 control-label">产品类别:</label>
                                    <div class="col-lg-2">
                                        <select id="label" name="label" class="selectpicker form-control" multiple data-live-search="false">
                                            {
                                                string[] labels = Model.label == null ? new string[] { } : Model.label.Split(',');

                                                foreach (var item in categories)
                                                {
                                                    if (labels != null && labels.Contains(item.id.ToString()))
                                                    {
                                                        <option selected value="@(item.id)" data-tokens="@(item.id)">@(item.name)</option>
                                                    }
                                                    else
                                                    {
                                                        <option value="@(item.id)" data-tokens="@(item.id)">@(item.name)</option>
                                                    }
                                                }
                                            }
                                        </select>
                                    </div>
                                </div>*@

                            @*<div class="form-group">
                                    <label for="RoleId" class="col-lg-2 control-label">群管微信昵称:</label>
                                    <div class="col-lg-2">
                                        <input type="text" id="group_wx_nickname" name="group_wx_nickname" value="@(Model.group_wx_nickname)" class="form-control" placeholder="输入群管微信昵称">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="RoleId" class="col-lg-2 control-label">群管机器人昵称:</label>
                                    <div class="col-lg-2">
                                        <input type="text" id="group_robot_name" name="group_robot_name" value="@(Model.group_robot_name)" class="form-control" placeholder="输入群管机器人昵称">
                                    </div>
                                </div>*@

                            <div class="form-group">
                                <div class="col-lg-offset-2 col-lg-10">
                                    <button class="btn btn-primary" id="submit" type="submit">@(Model.promotion == 1 ? "更新代理" : "开启代理")</button>

                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>